---@class TBS.Player:TBS.Actor
---@field New fun():TBS.Player
local Player = TBS.CreateClass("Player","Actor")

function Player:OnCreate()
    self.modelId = 1
    self.picFrame = 0
    self.hp = 100
    self.hpMax = 100
    self.groupId = 0
    self.groupIndex = 0
    self.playerId = 0   --玩家id（组）
    ---@type TBS.Player[]
    self.targetList = {}
    ---@type Spine.EventData
    self.animEventData = Spine.EventData.New("");
    self.skillMgr = TBS.SkillMgr.New()
    self.skillMgr:OnCreate(self)
    if TBS.Battle.isDebug then
        ---@type GameObject
        self.gameObject = UnityEngine.GameObject.New("player")
        TBS.Battle.AddToScene(self.gameObject)
    end
end

function Player:OnDestroy()
    TBS.EffectMgr.DestroyEffectByCaster(self:GetId())
    TBS.BufferMgr.RemoveBuff(self:GetId(),nil,true)
    if self.skillMgr then
        self.skillMgr:OnDestroy()
    end
    self.skillMgr = nil
    if self.gameObject then
        destroy(self.gameObject)
    end
    self.gameObject = nil
    self.transform = nil
end

function Player:Reborn()
    self.isDead = false
    self:SetHp(self.hpMax)
    TBS.BufferMgr.RemoveBuff(self:GetId())
    TBS.EffectMgr.DestroyEffectByCaster(self:GetId())
end
function Player:SetModel(id)
    self.modelId = id
end
function Player:GetModelId()
    return self.modelId
end
function Player:GetPicFrame()
    return self.picFrame
end
function Player:SetGroup(id,index)
    self.groupId = id
    self.groupIndex = index
end
function Player:GetGroupId()
    return self.groupId
end
function Player:GetPlayerId()
    return self.playerId
end
function Player:GetGroupIndex()
    return self.groupIndex
end
--回合开始
function Player:TurnStart()
    self.skillMgr:TurnStart()
    TBS.BufferMgr.TurnStart(self.id)
end
function Player:CanBeDestroyed()
    return self.isCanBeDestroyed
end
function Player:SetCanBeDestroyed(destroy)
    self.isCanBeDestroyed = destroy
end

function Player:EndAllSkill()
    self.skillMgr:EndAllSkill()
end
---@return TBS.Skill
function Player:CreateSkill(skillId,uniqueId)
    local skill = self.skillMgr:GetSkill(uniqueId)
    if not skill then
        skill = self.skillMgr:CreateSkill(skillId,uniqueId)
    end
    return skill
end
function Player:GetSkill(uniqueId)
    return self.skillMgr:GetSkill(uniqueId)
end
---@return TBS.Skill
function Player:DoSkill(uniqueId)
    local skill = self.skillMgr:GetSkill(uniqueId)
    if not skill then
       return
    end
    self.skillMgr:DoSkill(uniqueId)
    return skill
end
function Player:EndSkill(uniqueId)
   self.skillMgr:EndSkill(uniqueId)
end
--受到伤害
---@param attackPlayer TBS.Player
function Player:Hurt(damage,damageType,attackPlayer,sourceSkillId)
    if damageType == TBS.Config.DamageType.heal then
        damage = -damage
    end
    if self.isDead then return end
    local hp = math.max(0,self.hp - damage)
    hp = math.min(hp,self.hpMax)
    self:SetHp(hp)
    if self.hp <= 0 then
        self:Dead(attackPlayer)
    end
    FireEvent(TBS.Config.Event.PlayerHpChanged,self,attackPlayer)
    FireEvent(TBS.Config.Event.DamageShow,self,attackPlayer,damage,damageType,sourceSkillId,self.pos.x,self.pos.y)
end

---@param attackPlayer TBS.Player
function Player:Dead(attackPlayer)
    if self.isDead then return end
    self:SetHp(0)
    self.skillMgr:EndAllSkill(true)
    TBS.BufferMgr.RemoveBuff(self.id,nil,true)
    self.isDead = true
    FireEvent(TBS.Config.Event.ActorDead,self,attackPlayer)
end
function Player:SetHp(hp)
    if hp <= 0 then
        hp = 0
    end
    self.hp = hp
end

function Player:SetHpMax(hpMax)
    self.hpMax = hpMax
end
function Player:GetHp()
    return self.hp
end
function Player:GetHpMax()
    return self.hpMax
end
---@param target TBS.Player
function Player:SetTarget(target)
    table.clear(self.targetList)
    self.targetList[target:GetId()] = target
end
---@return TBS.Player
function Player:GetTarget()
    for k,v in pairs(self.targetList) do
        return v
    end
    return nil
end
function Player:SetPos(x, y)
    self.pos:Set(x,y,0)
    if TBS.Battle.isDebug then
        Util.Lua_SetLocalPositionXYZ(self.gameObject,x,y,0)
    end
end
function Player:ClearTarget()
    table.clear(self.targetList)
end
---@param target TBS.Player
function Player:AddTarget(target)
    if not target then return end
    if self.targetList[target:GetId()] then
        return
    end
    self.targetList[target:GetId()] = target
end
---@return TBS.Player[]
function Player:GetTargetList()
    return self.targetList
end
function Player:IsBoss()
    return self:IsType(TBS.Config.ActorType.boss)
end
function Player:IsLeader()
    return self:IsType(TBS.Config.ActorType.leader)
end
function Player:IsPlayer()
    return self:IsType(TBS.Config.ActorType.player)
end
function Player:GetHeroType()
    local cfg = dataTable.getDataTableByStringId("t_hero",self.modelId)
    if not cfg then return -1 end
    return cfg.f_HeroType
end
--- 0~1
function Player:GetHpRate()
    return self.hp/self.hpMax
end

function Player:AddBuff(buffId,overlay)
    if self.isDead then return nil end
    local buff = TBS.BufferMgr.GetBuff(self.id,buffId)
    if not buff then
        buff = TBS.BufferMgr.CreateBuff(buffId,overlay,0,self.id)
    else
        buff:OnBuffRefresh(overlay)
    end
    return buff
end
function Player:RemoveBuff(buffId)
    TBS.BufferMgr.RemoveBuff(self.id,buffId)
end
function Player:Update(dt)
    self.skillMgr:Update(dt)
end